package com.mianle.userapi.util.communion.redis.impl;


import com.mianle.userapi.util.communion.redis.LBSService;
import com.mianle.userapi.util.communion.redis.RedisConnection;
import com.mianle.userapi.util.communion.redis.domain.GeoCoordinate;
import com.mianle.userapi.util.communion.redis.domain.Postion;
import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.geo.GeoRadiusParam;

import java.util.ArrayList;
import java.util.List;

/**
 * 位置服务实现类
 *
 * @author 杨高超
 * @since 2017-12-26
 */
public class LBSServiceRedisImpl implements LBSService {
    private RedisConnection redisConnection;

    private Integer dbIndex;

    public void setRedisConnection(RedisConnection redisConnection) {
        this.redisConnection = redisConnection;
    }

    public void setDbIndex(Integer dbIndex) {
        this.dbIndex = dbIndex;
    }

    public boolean addPostion(Postion postion) {
        Jedis jedis = redisConnection.getJedis();
        try {
            return (1L == jedis.geoadd(postion.getType(),
                    postion.getCoordinate().getLongitude(),
                    postion.getCoordinate().getLatitude(),
                    postion.getId()));
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public List<Postion> radious(String type, GeoCoordinate center, Long distinct, Boolean asc) {
        List<Postion> postions = new ArrayList<Postion>();
        Jedis jedis = redisConnection.getJedis();
        try {
            GeoRadiusParam geoRadiusParam = GeoRadiusParam.geoRadiusParam().withCoord().withDist();
            if (asc) {
                geoRadiusParam.sortAscending();
            } else {
                geoRadiusParam.sortDescending();
            }
            List<GeoRadiusResponse> responses = jedis.georadius(type,
                    center.getLongitude(),
                    center.getLatitude(),
                    distinct.doubleValue(),
                    GeoUnit.M,
                    geoRadiusParam);
            if (responses != null) {
                for (GeoRadiusResponse response : responses) {
                    Postion postion = new Postion(response.getMemberByString(),
                            type,
                            response.getCoordinate().getLongitude(),
                            response.getCoordinate().getLatitude());
                    postion.setDistinct(response.getDistance());
                    postions.add(postion);
                }
            }
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }

        return postions;
    }

    /**
     * 根据名字获取坐标
     * @param name
     * @return
     */
    @Override
    public List<Postion> getPostionsByName(String name) {
        Jedis jedis = redisConnection.getJedis();
        try {
            String[] strings = {"张章", "张三", "李四"};
            List<redis.clients.jedis.GeoCoordinate> geoCoordinates = jedis.geopos("wt028e", strings);
            for(redis.clients.jedis.GeoCoordinate object : geoCoordinates) {
                System.out.println("经度="+object.getLongitude() + "纬度="+object.getLatitude());
            }

        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return null;
    }





}
